package com.kmust.dream.wanderingdream.donation.controller;

import java.util.Arrays;
import java.util.Map;

import com.kmust.dream.wanderingdream.donation.feign.UploadAdvice;
import io.swagger.annotations.Api;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import com.kmust.dream.wanderingdream.donation.entity.OthersInformationDonationEntity;
import com.kmust.dream.wanderingdream.donation.service.OthersInformationDonationService;
import com.kmust.dream.wanderingdream.comm.utils.PageUtils;
import com.kmust.dream.wanderingdream.comm.utils.R;
import org.springframework.web.multipart.MultipartFile;


/**
 * 
 *
 * @author Leng
 * @email Leng81605@gmail.com
 * @date 2021-07-12 21:35:52
 */
@RestController
@Api(tags = "受捐赠者其它信息")
@RequestMapping("donation/othersinformationdonation")
public class OthersInformationDonationController {
    @Autowired
    private OthersInformationDonationService othersInformationDonationService;

    /**
     * 列表
     */
    @GetMapping("/list")

    public R list(@RequestParam Map<String, Object> params){
        PageUtils page = othersInformationDonationService.queryPage(params);

        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @GetMapping("/info/{othersId}")

    public R info(@PathVariable("othersId") Integer othersId){
		OthersInformationDonationEntity othersInformationDonation = othersInformationDonationService.getById(othersId);

        return R.ok().put("othersInformationDonation", othersInformationDonation);
    }

    /**
     * 保存
     */
    @Autowired
    UploadAdvice uploadAdvice;
    @PostMapping("/save")

    public R save(OthersInformationDonationEntity othersInformationDonationEntity,@RequestParam("file") MultipartFile multipartFile){
        String url = uploadAdvice.getUrl(multipartFile);
        othersInformationDonationEntity.setOthersImg(url);
        othersInformationDonationService.save(othersInformationDonationEntity);
        return R.ok();
    }

    /**
     * 修改
     */
    @PutMapping("/update")

    public R update(OthersInformationDonationEntity othersInformationDonation,@RequestParam("file") MultipartFile multipartFile){
        if (!multipartFile.isEmpty()){
            othersInformationDonation.setOthersImg(uploadAdvice.getUrl(multipartFile));
        }
        System.out.println(othersInformationDonation);
		othersInformationDonationService.updateById(othersInformationDonation);

        return R.ok();
    }

    /**
     * 删除
     */
    @DeleteMapping("/delete")

    public R delete(@RequestBody Integer[] othersIds){
		othersInformationDonationService.removeByIds(Arrays.asList(othersIds));

        return R.ok();
    }
    @GetMapping("/getByRid/{rid}")
    public OthersInformationDonationEntity getByRId(@PathVariable("rid") String rid){
        return othersInformationDonationService.getByRId(rid);
    }

}
